package com.globallogic.training.javase.university;

import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.lang.StringUtils;

import com.globallogic.training.javase.university.member.Group;
import com.globallogic.training.javase.university.member.Teacher;

/**
 * This class represents the list of faculties that are available
 * in the university. The faculty comprises several groups.
 * @author Ivan Moisiienko
 * @version 0.2 2013/05/13
 */
public class Faculty {

	private String facultyId;
	private Set<Group> facultyGroups = new TreeSet<Group>();
	private Set<Teacher> facultyTeachers = new TreeSet<Teacher>();
	
	/**
	 * @param facultyId Faculty ID cannot be null or blank
	 */
	public Faculty(String facultyId) {		
		if (facultyId == null) {
			throw new IllegalArgumentException("Faculty ID is null");
		}
		
		if (StringUtils.isBlank(facultyId)) {
			throw new IllegalArgumentException("Faculty ID is blank");
		}
		
		this.facultyId = facultyId;
	}
	
	public String getFacultyId() {
		return facultyId;
	}

	public boolean addGroup(Group g) {
		if (g == null) {
			return false;
		}
		
		return facultyGroups.add(g);
	}
	
	public boolean removeGroup(Group g) {
		return facultyGroups.remove(g);
	}
	
	public Group getGroup(String groupId) {
		for (Group gr : facultyGroups) {
			if (gr != null && gr.getGroupId().equals(groupId)) {
				return gr;
			}
		}
		return null;
	}
	
	public Set<Group> getGroups() {
		return facultyGroups;
	}
	
	public boolean addTeacher(Teacher t) {
		if (t == null) {
			return false;
		}
		
		return facultyTeachers.add(t);
	}
	
	public boolean removeTeacher(Teacher t) {
		return facultyTeachers.remove(t);
	}
	
	public Teacher getTeacher(String fullName) {
		for (Teacher t : facultyTeachers) {
			if (t != null && t.getFullName().equals(fullName)) {
				return t;
			}
		}
		return null;
	}
	
	public Set<Teacher> getTeachers() {
		return facultyTeachers;
	}
	
	public String toString() {
		return "Faculty[ID: " + facultyId + "].";
	}

}
